# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=syncthing
pkgver=1.27.1
pkgrel=1
pkgdesc="Open Source Continuous File Synchronization"
url="https://syncthing.net/"
arch="all"
license="MPL-2.0"
pkgusers="$pkgname"
pkggroups="$pkgname"
makedepends="go"
install="$pkgname.pre-install"
subpackages="$pkgname-doc $pkgname-utils $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/syncthing/syncthing/archive/v$pkgver.tar.gz
	only-test-with-race-when-provided.patch
	$pkgname.initd
	"
options="chmod-clean"

# secfixes:
#   1.15.1-r0:
#     - CVE-2021-21404

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

case "$CARCH" in
# arm: oom on tests
# s390x: tests time out
arm*|s390x) options="$options !check" ;;
esac

build() {
	# Build syncthing + server utils
	local cmd
	for cmd in syncthing stdiscosrv strelaypoolsrv strelaysrv; do
		echo "Compiling $cmd"
		go run build.go -no-upgrade -version=v$pkgver install $cmd
	done
}

check() {
	# shellcheck disable=2046
	# very flake
	go test $(go list ./lib/... ./cmd/... | grep -Ev '(lib/model)')
}

package() {
	install -d -m711 -o $pkgname -g $pkgname "$pkgdir"/var/lib/$pkgname
	install -D -m755 "$builddir"/bin/$pkgname "$pkgdir"/usr/bin/$pkgname
	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname

	# Desktop files so people in Desktop Environments can put them on Autostart
	install -Dm0644 etc/linux-desktop/*.desktop -t "$pkgdir"/usr/share/applications

	local i
	for i in 32 64 128 256 512; do
		install -Dm644 "$builddir"/assets/logo-"$i".png \
			"$pkgdir"/usr/share/icons/hicolor/"$i"x"$i"/apps/$pkgname.png
	done
	install -Dm644 "$builddir"/assets/logo-only.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/$pkgname.svg

	# man pages
	cd "$builddir/man"
	local file
	for file in *.1; do
		install -Dm644 "$file" "$pkgdir"/usr/share/man/man1/"$file"
	done
	for file in *.5; do
		install -Dm644 "$file" "$pkgdir"/usr/share/man/man5/"$file"
	done
	for file in *.7; do
		install -Dm644 "$file" "$pkgdir"/usr/share/man/man7/"$file"
	done
}

utils() {
	cd "$builddir/bin"
	pkgdesc="Syncthing server utilities"
	for i in *; do
		if ! [ "$i" = "$pkgname" ]; then
			install -Dm 755 "$builddir"/bin/"$i" "$subpkgdir"/usr/bin/"$i"
		fi
	done
}

sha512sums="
048c8789a695d6c7a4f22f15b8f1c57241e9722fd72bda53e26d772bb4ad47c7003e859fbdc42f8f52a56394ff59a5aaff48bc259c3f0b9e876b36691ec17885  syncthing-1.27.1.tar.gz
81bcb6b2e0956624b596201d1de24a3b6fcb10d08761f2c426081350b611295a7f4d47775d175f2ee5dbbb289b98bc022389fc9992f0d31bcdbfde855ceafaf8  only-test-with-race-when-provided.patch
b001d1550f5877dd3a0e6506adc4cc451d08230269842064b3f6ad2e6919aca71edd8e46e3516395cd76f00505641f095df0e0bde60accb642708fc781d6347a  syncthing.initd
"
